/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netcracker.test.eleven;

//import EDU.oswego.cs.dl.util.concurrent.FJTask;
//import EDU.oswego.cs.dl.util.concurrent.FJTaskRunnerGroup;
import com.sun.org.apache.xerces.internal.impl.dv.xs.FullDVFactory;
import java.awt.Toolkit;
import java.util.Arrays;
import java.util.Collections;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.CORBA.MARSHAL;
import java.awt.Toolkit;
import java.util.*;

/**
 *
 * @author I
 */
public class Task11 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      

     /*
      * 2.1 задание
      */
        System.out.println("1.	С использованием только средств языка, классов Thread, Runnable");
            int[] mas = new int[] { 4, 3, 1, 5, 10, 6, 8, 2, 5, 9, 0 };
            int m = (mas.length)/2;
            MergeSortThread1 mg1 = new MergeSortThread1(Arrays.copyOfRange(mas, 0, m));
            MergeSortThread2 mg2 = new MergeSortThread2(Arrays.copyOfRange(mas, m, mas.length));
            Thread th1 = new Thread(mg1);
            Thread th2 = new Thread(mg2);
            th1.start();
            th2.start();
          
            while(true)
            {
                if(!th1.isAlive()&&!th2.isAlive()){
                    mas=MergeSort.merge(mg1.getArray(), mg2.getArray());
                    break;
                }
    
            }
            for(int i:mas)
                System.out.print(i+" ");
            System.out.println();
          
    /**
    * 2.2задание
    */
            System.out.println("2.	C использованием инcтрументов java.util.concurrent");
            Integer[] mas1 = new Integer[] { 4, 3, 1, 5, 10, 6, 8, 2, 5, 9, 0 };
            int m1 = (mas1.length)/2;
            MergeSortCallable1 mgc1 = new MergeSortCallable1(Arrays.copyOfRange(mas1, 0, m1));
            MergeSortCallable2 mgc2 = new MergeSortCallable2(Arrays.copyOfRange(mas1, m1, mas1.length));
            ExecutorService es = Executors.newFixedThreadPool(2);
            Future<Integer[]> fl1 = es.submit(mgc1);
            Future<Integer[]> fl2 = es.submit(mgc2);
            while(!fl1.isDone()||!fl2.isDone()) {
                 //подождать пока задача не выполнится
            }
            try {
                mas1 = mgc1.IntToInteger(MergeSort.merge(mgc1.integerToInt(fl1.get()), mgc2.integerToInt(fl2.get())));
            } catch (InterruptedException ex) {
                Logger.getLogger(Task11.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(Task11.class.getName()).log(Level.SEVERE, null, ex);
            }
            es.shutdown();
             for(Integer i:mas1)
                System.out.print(i.intValue()+" ");
              System.out.println();
     /**
      * 2.3 задание
      */
             System.out.println("3.	При помощи fork/join framework (требует JDK 7)");
             int[] mas2 = new int[] { 4, 3, 1, 5, 10, 6, 8, 2, 5, 9, 0 };
             ForkJoinPool fj = new ForkJoinPool();
             mas2 = fj.invoke(new MergeSortForkJoin(mas2));
             for(int i:mas2)
                System.out.print(i+" ");
              System.out.println();
    /**
     * 2.4 задание
     */
//             System.out.println("4.	С использованием библиотеки FJTask ");
//             int[] mas3 = new int[] { 4, 3, 1, 5, 10, 6, 8, 2, 5, 9, 0 };
//             FJTaskRunnerGroup g = new FJTaskRunnerGroup(Runtime.getRuntime().availableProcessors());
//             MergeSortFJTask[] task = new MergeSortFJTask[2];
//             int m3 = (mas3.length)/2;
//             task[0] = new MergeSortFJTask(Arrays.copyOfRange(mas3, 0, m3));
//             task[1] = new MergeSortFJTask(Arrays.copyOfRange(mas3, m3, mas3.length));
//            try {
//                g.invoke(new FJTask.Par(task));
//                mas3=MergeSort.merge(task[0].getA(), task[1].getA());
//                for(int i:mas3)
//                    System.out.print(i+" ");
//                
//            } catch (InterruptedException ex) {
//                Logger.getLogger(Task11.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            System.out.println();
    /**
     * 3 задание
     */
            System.out.println("Написать способ выполнить произвольный код при завершении работы JVM");
            ShutdownHook sdh = new ShutdownHook();
            Runtime.getRuntime().addShutdownHook(sdh);
            
    }
}
